require(['init'], function () {

  var $pagination = $('#js-pagination')
  var pagination

  function getCourseName(courseTypes) {
    var courseNames = '';
    $.each(courseTypes, function (i, item) {
      if(i != 0) {
        courseNames += ', '
      }
      courseNames += item.coursetypename;
    })
    return courseNames;
  }
  //导入
  function fileUpload($container) {
      var $uploadInput = $container.find('input')
      var coverUploadInput = new UploadInput({
        formData: {
          importer: 'teacher'
        },
        $dom: $uploadInput,
        done: function (uploadSrc) {
	  if(uploadSrc!="true"){
            var $import=$('<div style="padding-left:18px;margin-top:15px;"></div>').html('');
            $.each(uploadSrc,function(index,exce){
               $("<p style='height:30px;line-height:30px;'><label style='display:inline-block;width:85px;'>"+exce.student.name+":</label><label>"+exce.error+"</label></p>").appendTo($import);
            })
          }else{
            var $import=$('<div style="padding-left:18px;margin-top:15px;">数据已经导入成功！</div>');
          }
          var popUpBox = new PopUpBox({
              type: 'dom',
              style: 'default contentScroll',
              submitBtnType: 'confirm',
              keySubmit: false,
              titleText: '导入信息提示',
              show: true,
              content: $import,
              callback: {
                  yes: function (e) {
                    window.location.reload();
                  }
              },
              position: {
                  width:450,
                  height:263
              }
          })
        }
      });
  }

  fileUpload($('#J_container'))

  function getTeacherList(querystr, cb) {
    var url = '/api/teachers';
    var query = {}
    if(querystr) {
      query = querystr
    }

    ajaxMethod({
      url: url,
      data: query,
      success: function (data) {
        cb && cb(data)
        if(data.list.length == 0) {
          $('#J_teacherlist').html('<div style="margin-top: 50px;text-align: center;">无结果</div>');
          $pagination.hide()
          return;
        }
        var $teacherListTable = $('#J_teacherlist').html('');

        var $thead = $('<thead>\
          <tr>\
            <th>'+i18next.t('table.no')+'</th>\
            <th>'+i18next.t('table.name')+'</th>\
            <th>'+i18next.t('info.sex')+'</th>\
            <th>'+i18next.t('info.cellphonenumber')+'</th>\
            <th>'+i18next.t('info.classinaccountnumber')+'</th>\
            <th>'+i18next.t('info.homepage')+'</th>\
            <th>'+i18next.t('info.coursetype')+'</th>\
            <th>'+i18next.t('info.headteachers')+'</th>\
            <th style="width:200px;">'+i18next.t('table.handle')+'</th>\
          </tr>\
        </thead>').appendTo($teacherListTable)

        var $tbody = $('<tbody></tbody>').appendTo($teacherListTable)
        var yes = '<span class="btn text-primary">'+i18next.t('info.yes')+'</span>';
        var no = '<span class="btn text-danger">'+i18next.t('info.no')+'</span>';
        $.each(data.list, function (index, item) {
          var tuid = item.teacheruid
          function showhome() {
            if(item.status==1){
              return yes
            }else{
              return no
            }
          }
          var $teacherTr = $('<tr></tr>')
          $teacherTr.append('<td>'+ (index+1) +'</td>')
          $teacherTr.append('<td>'+ item.lastname +'</td>')
          $teacherTr.append('<td>'+ (item.sex == 0 ?i18next.t('sex.man'): i18next.t('sex.woman')) +'</td>')
          $teacherTr.append('<td>'+ item.mobile +'</td>')
          $teacherTr.append('<td>'+ item.ClassInNo +'</td>')
          $teacherTr.append('<td>'+ showhome() +'</td>')
          $teacherTr.append('<td>'+ getCourseName(item.coursedesc) +'</td>')
          $teacherTr.append('<td>\
            <div class="icon icon-size-40 icon-avatar">\
              <img src="'+ (item.photo|| 'img/avatar.png') +'">\
            </div>\
          </td>')

          var $handleTd = $('<td></td>').appendTo($teacherTr)
          var $editbtn = $('<a href="teacher.html?tuid='+ tuid +'" class="ml5"><span class="btn btn-primary btn-sm btn-inverse">'+i18next.t('table.compile')+'</span></a>').appendTo($handleTd)
          var $timebtn = $('<a href="teacher_worktime.html?tuid=' + tuid + '" class="ml5"><span class="btn btn-primary btn-sm btn-inverse">'+i18next.t('index.worktime')+'</span></a>').appendTo($handleTd)
          var $deletebtn = $('<a href="javascript:;"><span class="btn btn-danger btn-sm btn-inverse">'+i18next.t('table.delete')+'</span></a>').appendTo($handleTd)
          $deletebtn.on('click', function() {
            var popUpBox = new PopUpBox({
              type: 'dom',
              style: 'default contentScroll',
              submitBtnType: 'confirm',
              titleText: i18next.t('popupbox.systemmessages'),
              show: true,
              content: $('<div style="margin-left:27px;margin-top:34px;">'+i18next.t('popupbox.del')+'</div>'),
              callback: {
                yes: function(e) {
                  $.ajax({
                    url: '/api/teacher/' + tuid,
                    type: "delete",
                    success: function(data) {
                    var popUpBox = new PopUpBox({
                      type: 'dom',
                      style: 'default contentScroll',
                      submitBtnType: 'confirm',
                      titleText:i18next.t('popupbox.systemmessages'),
                      show: true,
                      content: "<div style='margin-left:27px;margin-top:34px;'>"+data.status.message+"!</div>",
                      callback: {
                        yes: function(e) {
                            pagination.updateData()
                        }
                      },
                      position: {
                        width: 448,
                        height: 226
                      }
                    })
                    }
                  });
                }
              },
              position: {
                width: 448,
                height: 226
              }
            })
          })
          $tbody.append($teacherTr)
        })

        $pagination.show()
      }
    })
  }

  function loadSearchData(querystr) {
    if(querystr) {
      pagination.filter.ClassInNo = querystr
      pagination.filter.lastname = querystr
      pagination.filter.mobile = querystr

      pagination.searchData()
    } else {
      pagination.reloadData()
    }
  }

  $('#J_teacher_search').on('click', function() {
    var $input = $(this).prev();
    var querystr = $.trim($input.val());
    loadSearchData(querystr)
  })

  $('.searchInput').on('keyup', function(e) {
    if(e.keyCode == 13) {
      var querystr = $.trim($(this).val());
      loadSearchData(querystr)
    }
  })


  pagination = new Pagination($pagination, getTeacherList)

})
